﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BobcatWiiLib;

namespace MultiTouchSource_WiimoteIR
{
    /// <summary>
    /// This control shows the custom config for the Wiimote IR source. It will be shown
    /// as part of the MultiTouchConfig screen. For the Wiimote IR you can configure:
    /// - The smoothing level.
    /// - Whether to force a re-calibration
    /// </summary>
    public partial class ConfigControl : UserControl
    {
        #region Public methods
        
        /// <summary>
        /// Constructor.
        /// </summary>
        public ConfigControl(WiimoteIRSource source)
        {
            m_Source = source;
            InitializeComponent();
        }

        #endregion

        #region Private functions

        /// <summary>
        /// Called when the screen is loaded. We show the initial params.
        /// </summary>
        private void ConfigControl_Load(object sender, EventArgs e)
        {
            if (m_Source.getIRManager() != null)
            {
                txtSmoothingLevel.Text = m_Source.getIRManager().SmoothingLevel.ToString();
                chkRaiseMouseEvents.Checked = m_Source.getIRManager().RaiseMouseEvents;
            }
        }

        /// <summary>
        /// Called when the smoothing box is edited.
        /// </summary>
        private void txtSmoothingLevel_TextChanged(object sender, EventArgs e)
        {
            // Have we got a valid number?
            int iValue;
            bool bIsNumber = int.TryParse(txtSmoothingLevel.Text, out iValue);
            if (bIsNumber == false || iValue < 1)
            {
                txtSmoothingLevel.BackColor = Color.Pink;
                return;
            }
            txtSmoothingLevel.BackColor = Color.Empty;

            // We store it in the ir-manager...
            if (m_Source.getIRManager() != null)
            {
                m_Source.getIRManager().SmoothingLevel = iValue;
            }
        }

        /// <summary>
        /// Called when the calibrate button is pressed. We show the calibration screen.
        /// </summary>
        private void cmdCalibrate_Click(object sender, EventArgs e)
        {
            if (m_Source.getIRManager() != null)
            {
                m_Source.getIRManager().showCalibrationScreen();
            }
        }

        /// <summary>
        /// Called when the "Raise mouse events" check-box is changed.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void chkRaiseMouseEvents_CheckedChanged(object sender, EventArgs e)
        {
            // We store the new value...
            if (m_Source.getIRManager() != null)
            {
                m_Source.getIRManager().RaiseMouseEvents = chkRaiseMouseEvents.Checked;
            }
        }

        #endregion

        #region Private data

        private WiimoteIRSource m_Source = null;

        #endregion

    }
}
